/**
 * Created by xinxinran on 2016/12/14.
 */
'use strict';
const express = require('express'),
    co = require('co'),
    router = express.Router(),
    Request = require('request');
router.get('/myName', function (req, res, next) {
    let nameInfo = (req.cookies['lg-pin'] || '|').split('|');
    res.json({loginName: nameInfo[0], userName: nameInfo[1]});
});

router.post('/testIf', function (req, res, next) {
    let reqObj = req.body, targetPath = reqObj.path, paramType = +reqObj.paramType, params = reqObj['params[]'],
        host = reqObj.host, tmpPName, targetUrl = 'http://' + host + targetPath, formObj;

    try {
        if (paramType) {
            Request.post({
                url: targetUrl,
                method: 'POST',
                json: true,
                body: JSON.parse(params)
            }).pipe(res);
        } else {
            formObj = {};
            params.forEach(p => {
                if (tmpPName) {
                    formObj[tmpPName] = p;
                    tmpPName = null;
                } else {
                    tmpPName = p;
                }
            });
            Request.post(targetUrl, {form: formObj}).pipe(res);
        }
    } catch (e) {
        res.json({code: 'FATAL-ERROR', msg: e});
    }

});

module.exports = router;